<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            height: 100vh;

        }

        canvas {
            margin: 0 auto;
            border: 1px solid #d9d9d9;
        }
    </style>
</head>

<body>
    <div id="radioBox">
        <input type="radio" value="no-repeat" name="patternRadio" checked> no-repeat
        <input type="radio" value="repeat" name="patternRadio"> repeat
        <input type="radio" value="repeat-x" name="patternRadio"> repeat-x
        <input type="radio" value="repeat-y" name="patternRadio"> repeat-y
    </div>

    <canvas id="canvas" width="800" height="600"></canvas>
    <script>
        const canvas = document.getElementById('canvas');
        const radioBox = document.getElementById('radioBox')
        const radios = document.getElementsByName('patternRadio')
        const ctx = canvas.getContext('2d');
        const patternImg = new Image()
        patternImg.src = 'pattern.jpg'
        patternImg.onload = function () {
            drawPattern('no-repeat')
        }


        radioBox.onclick = function (e) {
            radios.forEach(radio => {
                radio.checked && drawPattern(radio.value)
            })
        }

        function drawPattern(repeatType) {
            ctx.clearRect(0, 0, canvas.width, canvas.height)
            const pattern = ctx.createPattern(patternImg, repeatType);
            ctx.fillStyle = pattern
            ctx.fillRect(0, 0, canvas.width, canvas.height)
        }
    </script>
</body>

</html>